#include "route.h"


const std::string Route::XIAOH_ROUTE_LOGIN_CALLBACK         = "user/loginCallback";
const std::string Route::XIAOH_ROUTE_GET_USER_INFO_CALLBACK = "user/getUserInfoCallback";
const std::string Route::XIAOH_ROUTE_ADD_FRIEND_CALLBACK    = "user/addFriendCallback";
const std::string Route::XIAOH_ROUTE_FRIEND_LIST_CALLBACK   = "user/friendListCallback";
const std::string Route::XIAOH_ROUTE_CHAT_CALLBACK          = "user/chatCallback";
const std::string Route::XIAOH_ROUTE_RECV_CHAT_CALLBACK     = "user/recvChatCallback";
const std::string Route::XIAOH_ROUTE_REGISTER_CALLBACK      = "user/registeCallback";


Route::Route(QWidget *parent):QWidget(parent)
{
    this->myLoginInterface = nullptr;
    this->myMainWindowInterface = nullptr;
}

Route* Route::singleObj = NULL;
Route* Route::NewInstance(){
    if(singleObj == nullptr){
        singleObj = new Route();
    }

    return singleObj;
}

//把全局唯一的登陆界面对象设置到route的成员变量里面，api请求异步回来之后能通过成员变量操作到界面
void Route::setMyLoginInterface(MyLogin *mylogin){
    this->myLoginInterface = mylogin;
}

//同上设置全局唯一的mainwindow对象
void Route::setMyMainWindowInterface(MainWindow *myMainWindow){
    this->myMainWindowInterface = myMainWindow;
}

//分发收到的数据到各个handle
void Route::distributeRecvMsg(QByteArray recvBuffer){
    QString bufferQString = QString(recvBuffer);
    QJsonDocument doc = QJsonDocument::fromJson(bufferQString.toLocal8Bit().data());
    qDebug() << bufferQString<< endl;
    qDebug() << doc<< endl;
    if(doc.isNull() || doc.isEmpty()){
        std::cout << "json doc is NULL" << std::endl;
    }
    QJsonObject jsonObj = doc.object();
    if(jsonObj.contains("route")){//判断是否包含某个元素
        std::string route = jsonObj.take("route").toString().toStdString();
        int code = jsonObj.take("code").toInt();//获取某个元素
        QString msg = jsonObj.take("msg").toString();


        std::cout << route << std::endl;
        if(route == Route::XIAOH_ROUTE_LOGIN_CALLBACK){
            QJsonObject dataObj = jsonObj.take("data").toObject();
            QJsonObject dataObj2 = dataObj;
            //登陆回调
            responseLogin loginStruct;
            if(code == ResponseStruct::XIAOH_ROUTE_CODE_SUCCESS){
                loginStruct.uid = std::stoi(dataObj.take("id").toString().toStdString());
            }else{
                loginStruct.uid = 0;
            }

            responseUserInfo userinfoStruct;
            userinfoStruct = this->formateUserInfo(dataObj2);

            this->myMainWindowInterface->apiLoginCallback(code, msg, &loginStruct, userinfoStruct);
        }else if(route == Route::XIAOH_ROUTE_GET_USER_INFO_CALLBACK){
            QJsonObject dataObj = jsonObj.take("data").toObject();
            //获取用户信息回调
            responseUserInfo userinfoStruct;
            userinfoStruct = this->formateUserInfo(dataObj);

            this->myMainWindowInterface->apiGetUserInfoCallback(code, msg, &userinfoStruct);
        }else if(route == Route::XIAOH_ROUTE_ADD_FRIEND_CALLBACK){
            //添加好友回调

        }else if(route == Route::XIAOH_ROUTE_FRIEND_LIST_CALLBACK){
            QJsonArray dataArray = jsonObj.take("data").toArray();
            //好友列表回调
            responseGroupList groupList;
            groupList.groupName = "  我的好友";
            groupList.totalNum = dataArray.size();
            groupList.onlineNum = 0;
            for (int i = 0; i < dataArray.size(); i++) {
                responseUserInfo tmpUserInfo = this->formateUserInfo(dataArray[i].toObject());
                groupList.friendList.append(tmpUserInfo);
                if(tmpUserInfo.status == ResponseStruct::USER_STATUS_ONLINE){
                    groupList.onlineNum += 1;
                }
            }
            this->myMainWindowInterface->apiFriendList(code, msg, &groupList);
        }else if(route == Route::XIAOH_ROUTE_CHAT_CALLBACK){
            //聊天回调
            QJsonObject dataObj = jsonObj.take("data").toObject();

            this->myMainWindowInterface->chat->apiChatCallback(code, msg);
        }else if(route == Route::XIAOH_ROUTE_RECV_CHAT_CALLBACK){
            //其他人对我发送的聊天回调
            QJsonObject dataObj = jsonObj.take("data").toObject();

            responseRecvChat recvData;
            recvData.uid = dataObj.take("uid").toInt();
            recvData.beuid = dataObj.take("beuid").toInt();
            recvData.msg = dataObj.take("msg").toString();

            this->myMainWindowInterface->apiRecvChatCallback(code, msg, &recvData);
        }else if(route == Route::XIAOH_ROUTE_REGISTER_CALLBACK){
            //注册回调

        }else{
            //不存在的路由

        }

    }
}

//组装数据为userinfo结构
responseUserInfo Route::formateUserInfo(QJsonObject dataObj){
    responseUserInfo data;
    data.uid = std::stoi(dataObj.take("id").toString().toStdString());
    data.age = std::stoi(dataObj.take("age").toString().toStdString());
    data.sex = std::stoi(dataObj.take("sex").toString().toStdString());
    data.avatar =dataObj.take("avatar").toString();
    data.status = std::stoi(dataObj.take("status").toString().toStdString());
    data.username = dataObj.take("username").toString();
    data.signText = dataObj.take("sign_text").toString();
    if(data.status == ResponseStruct::USER_STATUS_ONLINE){
        data.statusText = "[在线]";
    }else{
        data.statusText = "[离线]";
    }

    return data;
}
